﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGinnBPM.MessageBus.Impl.HttpService;
using NGinnBPM.MessageBus;
using NLog;
using NGinnBPM.Lib.Interfaces;
using NGinnBPM.Runtime;
using NGinnBPM.Lib.Interfaces.Worklist;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;


namespace NGinnBPM.Services.Website
{
    [UrlPattern("^/nginfo/")]
    public class ConfigInfoServlet : ServletBase
    {
        public INGEngine Engine { get; set; }
        public IProcessPackageRepository PackageRepository { get; set; }
        public INGEnvironment Environment { get; set; }
        public IMessageBus MessageBus { get; set; }
        public IServiceResolver ServiceLocator { get; set; }

        public override void HandleRequest(IRequestContext ctx)
        {
            FSProcessPackageRepository repo = PackageRepository as FSProcessPackageRepository;
            ctx.ResponseContentType = "text/html";
            ctx.Output.WriteLine("<html><body>");
            ctx.Output.WriteLine("<h2>NGinn BPM configuration and current status</h2>");
            ctx.Output.WriteLine("<table border='1'>");
            ctx.Output.WriteLine("<tr><td>{0}</td><td align='right'>{1}</td></tr>", "Endpoint", MessageBus.Endpoint);
            ctx.Output.WriteLine("<tr><td>{0}</td><td align='right'>{1}</td></tr>", "Package repository type", PackageRepository.ToString());
            if (repo != null)
            {
                
                ctx.Output.WriteLine("<tr><td>{0}</td><td align='right'>{1}</td></tr>", "Package base directory", repo.BaseDirectory);
            }
            NGEnvironment en = Environment as NGEnvironment;
            ctx.Output.WriteLine("<tr><td>Registered worklist services</td><td><ul>");
            foreach (IWorkListService worklist in ServiceLocator.GetAllInstances<IWorkListService>())
            {
                RealProxy rp = RemotingServices.GetRealProxy(worklist);
                if (rp != null)
                {
                    ctx.Output.WriteLine("<li>remote: {0}</li>", rp.CreateObjRef(typeof(IWorkListService)).URI);
                }
                else
                {
                    ctx.Output.WriteLine("<li>{0}</li", worklist.ToString());
                }
            }
            ctx.Output.WriteLine("</ul></td></tr>");

            ctx.Output.WriteLine("<tr><td>Registered resource managers</td><td><ul>");
            foreach (IUserDataRepository worklist in ServiceLocator.GetAllInstances<IUserDataRepository>())
            {
                RealProxy rp = RemotingServices.GetRealProxy(worklist);
                if (rp != null)
                {
                    ctx.Output.WriteLine("<li>remote: {0}</li>", rp.CreateObjRef(typeof(IUserDataRepository)).URI);
                }
                else
                {
                    ctx.Output.WriteLine("<li>{0}</li", worklist.ToString());
                }
            }
            ctx.Output.WriteLine("</ul></td></tr>");
            

            ctx.Output.WriteLine("</table></body></html>");
        }
    }
}
